// let apiUrl = "http://192.168.0.136:8080/api";
// import {
//   serverpath
// } from './const';
// 接口基地址
import {
  useUsercodeStore
} from '@/store/usercode.js'
import {
  serverpath
} from './const.js';
const request = (config, isServerpath = true) => {
  let userCodeStore = useUsercodeStore()
  // 处理 apiUrl
  // const baseURL = 'https://app.oujiang.fun/' //旧正式
  const baseURL = serverpath //测试
  // const baseURL = 'https://ouhun.wm2177.com/' //正式/
  config.url = baseURL + config.url;
  // config.url = serverpath + config.url;
  if (!config.data) {
    config.data = {};
  }
  // const accountInfo = wx.getAccountInfoSync();
  // this.version = accountInfo.plugin ? accountInfo.plugin.version : '';
  // this.version1 = accountInfo.miniProgram.version ? accountInfo.miniProgram.version : '';
  config.header = {
    'user-id': userCodeStore.usercode.userId,
    'token': userCodeStore.usercode.token,
    ...config.header,
  }
  let promise = new Promise(function(resolve, reject) {
    uni.request(config).then(responses => {
      // 异常
      if (responses[0]) {
        reject({
          message: "网络超时"
        });
      } else {
        let response = responses.data // 如果返回的结果是data.data的，嫌麻烦可以用这个，return res,这样只返回一个data
        // console.log(response)
        // 没登录
        if (response.st == 401 && !userCodeStore.usercode.userId) {
          uni.utils.toast("请先登录")
          setTimeout(() => {
            // uni.navigateTo({
            // 	url: `/pages/login/login`,
            // })
          }, 2000)
        }
        // 账号封了
        else if (response.st == 403) {
          uni.utils.toast(response.msg)
        }
        //token过期
        else if (response.st == 402 && userCodeStore.usercode.userId) {
          uni.utils.toast(response.msg)
          userCodeStore.delUsercode()
          setTimeout(() => {
            uni.navigateTo({
              url: `/pages/login/login`,
            })
          }, 2000)
        } else {
          resolve(response);
        }

      }
    }).catch(error => {
      reject(error);
    })
  })
  return promise;
};

export default request